using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;

namespace FiltronASP.Models.DAO.Filtron
{
    public class FiltronModelDAO : Abstracts.AbstractGenericDAO<FiltronModel, System.Nullable<int>>,
        Interfaces.Filtron.IFiltronModelDAO
    {
        /// <summary>
        /// Metoda zwracajaca nazwe parametru bedacego kluczem glownym tabeli.
        /// </summary>
        /// <returns>nazwa klucza glownego</returns>
        protected override string GetKeyProperty()
        {
            return "model_id";
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie producenta.
        /// </summary>
        /// <param name="makeId">producent</param>
        /// <returns>rekordy o zadanym producencie lub null, gdy nie ma</returns>
        public IQueryable<FiltronModel> FindByMake(int makeId)
        {
            if (makeId.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<FiltronModel> models = this._cmsEntitiesDataContext.FiltronModels.Where(
                    mode => mode.make_id.Equals(makeId)
                        ).OrderBy(mode => mode.model_name);
                if (!(models.Any()))
                {
                    return null;
                }   //if
                return models;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Pobieranie obiektow na podstawie producenta i roku.
        /// </summary>
        /// <param name="makeId">producent</param>
        /// <returns>rekordy o zadanym producencie i roku lub null, gdy nie ma</returns>
        public IQueryable<FiltronModel> FindByMakeAndYear(int makeId, int year)
        {
            if (makeId.Equals(0) || year.Equals(0))
            {
                return null;
            }   //if
            try
            {
                IQueryable<FiltronModel> models = this._cmsEntitiesDataContext.FiltronModels.Where(
                    mode => mode.make_id.Equals(makeId) && (mode.model_year_from <= year)
                        && (mode.model_year_to >= year) 
                        );
                if (!(models.Any()))
                {
                    return null;
                }   //if
                return models;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }
    }
}
